// pages/blind_date/blind_date.js
import { Banner } from "../../model/banner";
import { UserAuth } from "../../model/user-auth";

const appInst =  getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    changeColor: appInst.globalData.changeColor,
    fontColor: appInst.globalData.fontColor,
    titleColor: appInst.globalData.titleColor,
    moreColor: appInst.globalData.moreColor,
    whiteColor: appInst.globalData.whiteColor,
    uaPage: 1,
    uaLimit: 5,
    uaLast: true,
    banner: [],
    user: [],
    gender: 0,
    sort_type: 1,
    sort_type_status: 'desc',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    wx.showLoading({
      title: '加载中',
    });
    this.getBannerList();
  },
  /**
   * 获取banner图
   */
  async getBannerList() {
    const res = await Banner.getDataList( 2)
    this.setData({
      banner: res.items,
    });
    this.getAuthList();
  },
  /**
   * 获取用户上墙列表
   */
  async getAuthList() {
    const res = await UserAuth.getDataList({
      page: this.data.uaPage,
      limit: this.data.uaLimit,
      gender: this.data.gender,
      sort_type: this.data.sort_type,
      sort_type_status: this.data.sort_type_status,
    });
    this.setData({
      user: res.data
    });
    wx.hideLoading();
  },
  /**
   * 点击跳转搜索页面
   */
  openSearch() {
    wx.navigateTo({
      url: 'search',
    })
  },
  /**
   * banner点击事件
   */
  openBanner(e) {
    appInst.bannerLinkTo(e);
  },
  /**
   * 点击出现详情
   */
  openWin(e) {
    wx.navigateTo({
      url: 'details?id='+e.currentTarget.dataset.id,
    })
  },
  /**
   * 切换性别
   */
  changeGender(e) {
    if(e.currentTarget.dataset.id === this.data.gender) {
      this.setData({
        gender: 0
      });
    } else {
      this.setData({
        gender: e.currentTarget.dataset.id
      });
    }
    wx.showLoading({
      title: '加载中',
    });
    this.getAuthList();
  },
  /**
   * 切换排序方式
   */
  changeSort(e) {
    // 选择当前选中的信息
    if(e.currentTarget.dataset.sort === this.data.sort_type) {
      if(e.currentTarget.dataset.sort === 1) {
        return false;
      } else {
          if(this.data.sort_type_status === 'desc') {
              this.setData({
                sort_type_status: 'asc'
              })
          } else {
            this.setData({
              sort_type_status: 'desc'
            })
          }
        wx.showLoading({
          title: '加载中',
        });
        this.getAuthList();
      }
    } else {
      this.setData({
        sort_type: e.currentTarget.dataset.sort
      });
      wx.showLoading({
        title: '加载中',
      });
      this.getAuthList();
    }
  }
})